﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace iMusik
{
    public partial class MediaControl : UserControl
    {
        private ImageBrush _playImage;
        private ImageBrush _pauseImage;
        private ImageBrush _loopImage;
        private ImageBrush _shuffleImage;

        public MediaControl()
        {
            InitializeComponent();
            _playImage = new ImageBrush();
            _pauseImage = new ImageBrush();
            _playImage.ImageSource = new BitmapImage(new Uri(@"/Assets/Icons/appbar.transport.pause.rest.png", UriKind.Relative));
            _pauseImage.ImageSource = new BitmapImage(new Uri(@"/Assets/Icons/appbar.transport.play.rest.png", UriKind.Relative));
            _loopImage = new ImageBrush();
            _shuffleImage = new ImageBrush();
            _loopImage.ImageSource = new BitmapImage(new Uri(@"/Assets/Images/loop.png", UriKind.Relative));
            _shuffleImage.ImageSource = new BitmapImage(new Uri(@"/Assets/Images/shuffle.png", UriKind.Relative));
        }

        public void SetPlayButton()
        {
            _borderPlay.Background = _playImage;
        }

        public void SetPauseButton()
        {
            _borderPlay.Background = _pauseImage;
        }

        public void SetLoopButton()
        {
            _btnLoop.Background = _loopImage;
        }

        public void SetShuffleButton()
        {
            _btnLoop.Background = _shuffleImage;
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            switch (MediaPlayer.State)
            {
                case MediaState.Playing:
                    MediaPlayer.Pause();
                    _borderPlay.Background = _pauseImage;
                    break;
                case MediaState.Paused:
                    MediaPlayer.Resume();
                    _borderPlay.Background = _playImage;
                    break;
                default:
                    break;
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayer.MovePrevious();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayer.MoveNext();
        }

        private void btnLoop_Click(object sender, RoutedEventArgs e)
        {
            if (MediaPlayer.IsShuffled)
            {
                MediaPlayer.IsShuffled = false;
                MediaPlayer.IsRepeating = true;
                _btnLoop.Background = _shuffleImage;
            }
            else if (MediaPlayer.IsRepeating)
            {
                MediaPlayer.IsRepeating = false;
                MediaPlayer.IsShuffled = true;
                _btnLoop.Background = _loopImage;
            }
            else
            {
                _btnLoop.Background = _shuffleImage;
                MediaPlayer.IsRepeating = true;
            }
        }

        private void border_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                (sender as Border).BorderBrush = new SolidColorBrush(Colors.Orange);
            }
            catch
            { }
        }

        private void border_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                (sender as Border).BorderBrush = new SolidColorBrush(Colors.White);
            }
            catch
            { }
        }
    }
}
